<template>
  <div class="login">
    <div class="login_bg_img">
      <img src="@/assets/image/login_01.jpg" />
    </div>
    <div class="main">
      <div class="top_view">
        <img src="@/assets/image/login_logo.png" alt="" class="img1" />
        <div></div>
        <span>实验室综合管理系统</span>
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            @focus="isUsername = true"
            @blur="isUsername = false"
            placeholder="用户名"
          >
            <svg-icon
              slot="prefix"
              icon-class="user"
              v-bind:class="{ 'color-class': isUsername }"
              class="input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @focus="isPassword = true"
            @blur="isPassword = false"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="password"
              v-bind:class="{ 'color-class': isPassword }"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            class="check_view"
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            @focus="isValidCode = false"
            @blur="isValidCode = false"
            @keyup.enter.native="handleLogin"
            style="width: 65%"
          >
            <svg-icon
              slot="prefix"
              icon-class="validCode"
              v-bind:class="{ 'color-class': isValidCode }"
              class="el-input__icon input-icon"
            />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" />
          </div>
        </el-form-item>
        <div class="exchange_view">
          <span @click="getCode">换一张</span>
        </div>
        <el-form-item style="width: 100%">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            class="login_button"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">LOGIN</span>
            <span v-else>LOGIN...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2022 南京邮电大学实验室建设与管理处（025-85866237/025-85866892）</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
//import { encrypt, decrypt } from "@/utils/aes.js";

export default {
  name: "Login",
  data() {
    return {
      isUsername: false,
      isPassword: false,
      isValidCode: false,
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "1",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
        ],
        code: [
          { required: true, trigger: "change", message: "验证码不能为空" },
        ],
      },
      loading: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    Cookies.remove("ssologin");
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
        //this.loginForm.code = '1234';
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
     // console.log("password:"+password);
      this.loginForm = {
        username: username === undefined ? this.loginForm.username.trim() : username.trim(),
        password:
          password === undefined ? this.loginForm.password : password,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", this.loginForm.password, {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
            })
            .catch(() => {
              this.loading = false;
              this.getCode();
            });
        }
      });
    },
    //点击忘记密码或注册
    clickBottomAction(index) {
      switch (index) {
        case 0:
          break;
        case 1:
          break;
      }
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #0d1d30;
  position: relative;
}

.login_bg_img {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0px;
  background-color: #fff;
  img {
    width: 100%;
  }
}

.main {
  position: relative;
  width: 620px;
  height: 450px;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-left: 20%;
  margin-bottom: 4%;
  .login-form {
    width: 300px;
  }
  .top_view {
    height: 80px;
    width: 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
    img {
      width: 40%;
    }
    div {
      height: 25px;
      width: 2px;
      background-color: #0150bd;
      margin-left: 20px;
      margin-right: 20px;
    }
    span {
      font-size: 30px;
      color: #1d3b84;
    }
  }
  .login_button {
    width: 100%;
    height: 45px;
    font-size: 16px;
    background: linear-gradient(
      180deg,
      rgb(248, 217, 143),
      rgb(222, 203, 110),
      rgb(248, 217, 143)
    );
    border: 1px solid #bbb;
    font-weight: 500;
    color: #333;
  }
  .exchange_view {
    width: 100%;
    display: flex;
    flex-direction: row-reverse;
    align-items: center;
    height: 30px;
    margin-top: -22px;
    span {
      font-size: 14px;
      color: #007bbb;
      margin-right: 23px;
    }
  }
  .bottom_view {
    width: 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    span {
      font-size: 16px;
      color: #007bbb;
    }
  }
}

.login-code {
  position: absolute;
  width: 88px;
  height: 32px;
  right: 0;
  bottom: 6px;
  img {
    width: 88px;
    height: 32px;
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 120px;
  width: 100%;
  background: linear-gradient(35deg, #0150bd 0%, #119fe2);
  display: flex;
  flex-direction: column;
  align-items: center;
  span {
    font-size: 16px;
    color: #6fd0fe;
    margin-top: 20px;
    font-weight: 500;
  }
}
</style>
